/*
    build_website.c
    Build a website
    BeginnerC
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include "../config/config.h"
#include "../build_single_book/build_single_book.h"
#include "../file/file.h"
#include "build_website.h"
#define BUFFER_LENGTH 2048
#define MDBOOK_COMPLETE_NAME "book"
#define MAIN_WEBSITE_DIRECTORY_NAME "index"
#define MAIN_WEBSITE_INDEX_FILE "index.html"
#define SUB_WEBSITE_DIRECTORY_NAME "books"
#define COMPLETE_FLAG "_complete"
typedef struct BuildInfo
{
    Config *config;
    char *target_path;
}BuildInfo;
/*
    BuildSubWebsite
        Identify directory and compile them to website
    Argument
        root_path
            The root path
        file_name
            The current file name
        type
            The file type, if the file is a directory, we will compile it
    Return Value
        No value will return
*/
void BuildSubWebsite(const char *root_path, const char *file_name, int type, void *ext_argument)
{
    char source_website_path[BUFFER_LENGTH] = {};
    char target_website_path[BUFFER_LENGTH] = {};
    char command[BUFFER_LENGTH] = {};
    if (DT_DIR != type)
    {
        return ;
    }
    if (!strcmp(file_name, ".") || !strcmp(file_name, ".."))
    {
        return ;
    }
    sprintf(source_website_path, "%s/%s", root_path, file_name);
    sprintf(target_website_path, "%s/%s", ((BuildInfo*)(ext_argument)) -> target_path, file_name);
    sprintf(command, "%s build '%s' -d '%s'", GetConfigValue(((BuildInfo*)(ext_argument)) -> config, MDBOOK_PATH), source_website_path, target_website_path);
    system(command);
    return ;
}
/*
    BuildWebsite
        Build a website
    Argument
        path
            The path to the src markdown path
    Return Value
        0
            Complete
*/
int BuildWebsite(const char *path, Config *config)
{
    char main_website_path[BUFFER_LENGTH] = {};
    char sub_website_path[BUFFER_LENGTH] = {};
    char target_website_path[BUFFER_LENGTH] = {};
    char source_path[BUFFER_LENGTH] = {};
    char target_path[BUFFER_LENGTH] = {};
    BuildInfo info = {};
    /* First: Buile the main framework & index website  */
    sprintf(target_website_path, "%s%s", path, COMPLETE_FLAG);
    if (FileExists(target_website_path))
    {
        DeleteDirectory(target_website_path);
    }
    CreateDirectory(target_website_path);
    /* Build the index */
    sprintf(source_path, "%s/%s", path, MAIN_WEBSITE_DIRECTORY_NAME);
    sprintf(target_path, "%s", target_website_path);
    CopyDirectory(source_path, target_path);
    // BuildSingleBook(source_path, target_path, config);
    /* Second: Build the sub website */
    sprintf(source_path, "%s/%s", path, SUB_WEBSITE_DIRECTORY_NAME);
    sprintf(target_path, "%s/%s", target_website_path, SUB_WEBSITE_DIRECTORY_NAME);
    CreateDirectory(target_path);
    info.config = config;
    info.target_path = target_path;
    ListDirectoryFile(source_path, BuildSubWebsite, &info);
    return 0;
}